package com.zat.mes.andrmes.dofun.work

import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.google.gson.Gson
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.customerview.BottomSignCommDialog
import com.zat.mes.andrmes.data.BadFlatItem
import com.zat.mes.andrmes.data.Itemm
import com.zat.mes.andrmes.data.LoginResultBean
import com.zat.mes.andrmes.data.ScheduClassBean
import com.zat.mes.andrmes.dofun.work.vm.BadFlatModel
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.PreferencesUtil
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.activity_bad_flat.*
import kotlinx.android.synthetic.main.activity_bad_flat.llBtnScheduNo
import kotlinx.android.synthetic.main.activity_bad_flat.llClass
import kotlinx.android.synthetic.main.activity_bad_flat.tbClass
import kotlinx.android.synthetic.main.activity_bad_flat.tvScheduNo
import kotlinx.android.synthetic.main.activity_deal_on_site.*
import kotlinx.android.synthetic.main.activity_disassemble.*
import kotlinx.android.synthetic.main.activity_yarn_in.*
import kotlinx.android.synthetic.main.layout_title.*
import org.json.JSONObject

class DisassembleActivity : BaseActivity() {
    var badFlatModel: BadFlatModel? = null
    var badFlatItem: BadFlatItem? = null
    var classBean: MutableList<ScheduClassBean>? = null
    var empBean: MutableList<ScheduClassBean>? = null
    override fun init() {
        initView()
        initData()
    }

    private fun initData() {

        badFlatModel = ViewModelProvider(this).get(BadFlatModel::class.java)
        badFlatModel?.getScheduClass()?.observe(this, Observer {
            classBean = it

        })
        badFlatModel?.getScheduEmp()?.observe(this, Observer {
            empBean = it
        })
        badFlatModel?.getBadFlat()?.observe(this, Observer {
            closeLoading()
            showToast(it)
        })


    }

    private fun initView() {
        setContentView(R.layout.activity_disassemble)
        tVtitle.text = "拆解单"
        rlReturnTitle.visibility = View.VISIBLE
        rlReturnTitle.setOnClickListener { finish() }
        val loginResultBean = Gson().fromJson(
            PreferencesUtil.getString(CompanionType.LOGIN_RESULT),
            LoginResultBean::class.java
        )
        var empId = loginResultBean.empid
        var classId = loginResultBean.classid
        llBtnScheduNo.setOnClickListener {
            startActivityForResult(Intent(this, SelectBadFlatActivity::class.java), 0)
        }
        btnDisass.setOnClickListener {
            if (badFlatItem == null) {
                showToast("请选择废片")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(etWorkTime.text.toString())) {
                showToast("请输入工时")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(etWage.text.toString())) {
                showToast("请输入工资")
                return@setOnClickListener
            }


            if (TextUtils.isEmpty(etYcQty.text.toString())) {
                showToast("请输入应拆数量")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(etScQty.text.toString())) {
                showToast("请输入实拆数量")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(etShQty.text.toString())) {
                showToast("请输入损耗数量")
                return@setOnClickListener
            }
            showLoading()
            val json = JSONObject()
            json.put("scrapsId", badFlatItem?.scrapsId)
            json.put("empId", empId)
            json.put("classId", classId)
            json.put("workTime", etWorkTime.text.toString())
            json.put("wages", etWage.text.toString())
            json.put("planDismantled", etYcQty.text.toString())
            json.put("realDismantled", etScQty.text.toString())
            json.put("wasteNumber", etShQty.text.toString())
            badFlatModel?.postDisasse(json.toString())
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            val item = data?.getParcelableExtra("badFlat") as BadFlatItem
            badFlatItem = item
            tvBadFlatName.text = "废片：" + item?.scrapsName

        }
    }


}
